package com.yserp.project.erp.basic.warehouse.controller;

import com.yserp.common.utils.poi.ExcelUtil;
import com.yserp.framework.aspectj.lang.annotation.Log;
import com.yserp.framework.aspectj.lang.enums.BusinessType;
import com.yserp.framework.web.controller.BaseController;
import com.yserp.framework.web.domain.AjaxResult;
import com.yserp.framework.web.page.TableDataInfo;
import com.yserp.project.erp.basic.warehouse.domain.BasisWarehouse;
import com.yserp.project.erp.basic.warehouse.service.IBasisWarehouseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 仓库管理Controller
 * 
 * @author yserp
 * @date 2022-06-29
 */
@Controller
@RequestMapping("/basic/warehouse")
public class BasisWarehouseController extends BaseController
{
    private String prefix = "erp/basic/warehouse";

    @Autowired
    private IBasisWarehouseService basisWarehouseService;

    @RequiresPermissions("basic:warehouse:view")
    @GetMapping()
    public String warehouse()
    {
        return prefix + "/warehouse";
    }

    /**
     * 查询仓库管理列表
     */
    @RequiresPermissions("basic:warehouse:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasisWarehouse basisWarehouse)
    {
        startPage();
        List<BasisWarehouse> list = basisWarehouseService.selectBasisWarehouseList(basisWarehouse);
        return getDataTable(list);
    }

    /**
     * 导出仓库管理列表
     */
    @RequiresPermissions("basic:warehouse:export")
    @Log(title = "仓库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BasisWarehouse basisWarehouse)
    {
        List<BasisWarehouse> list = basisWarehouseService.selectBasisWarehouseList(basisWarehouse);
        ExcelUtil<BasisWarehouse> util = new ExcelUtil<BasisWarehouse>(BasisWarehouse.class);
        return util.exportExcel(list, "仓库管理数据");
    }

    /**
     * 新增仓库管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存仓库管理
     */
    @RequiresPermissions("basic:warehouse:add")
    @Log(title = "仓库管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BasisWarehouse basisWarehouse)
    {
        return toAjax(basisWarehouseService.insertBasisWarehouse(basisWarehouse));
    }

    /**
     * 修改仓库管理
     */
    @RequiresPermissions("basic:warehouse:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BasisWarehouse basisWarehouse = basisWarehouseService.selectBasisWarehouseById(id);
        mmap.put("basisWarehouse", basisWarehouse);
        return prefix + "/edit";
    }

    /**
     * 修改保存仓库管理
     */
    @RequiresPermissions("basic:warehouse:edit")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BasisWarehouse basisWarehouse)
    {
        return toAjax(basisWarehouseService.updateBasisWarehouse(basisWarehouse));
    }

    /**
     * 删除仓库管理
     */
    @RequiresPermissions("basic:warehouse:remove")
    @Log(title = "仓库管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(basisWarehouseService.deleteBasisWarehouseByIds(ids));
    }


    /**
     * 校验仓库编码
     */
    @PostMapping("/checkWarehouseCodeUnique")
    @ResponseBody
    public String checkWarehouseCodeUnique(BasisWarehouse basisWarehouse) {
        return basisWarehouseService.checkWarehouseCodeUnique(basisWarehouse.getWarehouseCode());
    }


    /**
     * ajax查询仓库设置列表
     */
    @PostMapping("/ajaxList")
    @ResponseBody
    public AjaxResult ajaxList(BasisWarehouse basisWarehouse) {
        List<BasisWarehouse> list = basisWarehouseService.selectBasisWarehouseList(basisWarehouse);
        return AjaxResult.success(list);
    }

    /**
     * 选择仓库信息
     */
    @GetMapping("/selectWarehouse")
    public String selectWarehouse()
    {
        return prefix + "/selectWarehouse";
    }

    /**
     * 查询仓库信息列表(选择页面)
     */
    @PostMapping("/selectList")
    @ResponseBody
    public TableDataInfo selectList(BasisWarehouse basisWarehouse)
    {
        startPage();
        List<BasisWarehouse> basisWarehouses = basisWarehouseService.selectBasisWarehouseList(basisWarehouse);
        return getDataTable(basisWarehouses);
    }
}
